
import re
import logging
import datetime

import os
import logging
import new_dispatch.const.url_const as URL
import new_dispatch.const.api_const as API
import new_dispatch.const.log_const as LOG
from new_dispatch.util.request_util import requestUtil

class logginApi():

    def __init__(self):
        self.request_util = requestUtil()
        self.important_info = {}
        self.param_info = {}

    def baoxian_project_login(self, login_info):
        """
        保险大厅登陆接口
        :param login_info:
        :return:
        """
        url = URL.HOME_URL + "/api/loginByPwd"
        data = {
            "phone": login_info['username'],
            "password": login_info['password'],
            "nc_token": "",
            "nc_sig": "",
            "nc_csessionid": "",
            "loginRefere": ""

        }
        headers = {"Content-Type": "application/json"}
        response = self.request_util.request(url, API.POST, API.JSON, param=data, headers=headers)
        logging.info(msg='response: ' + str(response))
        self.important_info['login_token'] = response['result']['token']
        logging.info(msg='login_token: ' + str(self.important_info['login_token']))


    def choose_company(self, login_info):
        """
        选择公司接口
        :param customer_code: 公司编码 - int/str
        :return:
        """
        url = URL.HOME_URL + "/api/account/choseCompany"
        data = {
            "customer_code": login_info['customer_code']
        }
        headers = {"token": self.important_info['login_token']}
        response = self.request_util.request(url, API.POST, API.JSON, param=data, headers=headers)
        # print(response)
        logging.info(msg='response: ' + str(response))
        self.important_info['choose_token'] = response['result']['token']
        logging.info(msg='choose_token: ' + str(self.important_info['choose_token']))

    def get_token(self):
        """
        获取保险大厅登陆的token
        :return:
        """
        url = URL.HOME_URL + "/api/token"
        data = {
            "token": self.important_info['choose_token']
        }
        # headers = {"token": self.important_info['token']}
        response = self.request_util.request(url, API.GET, API.JSON, param=data)
        # print(response)
        logging.info(msg='response: ' + str(response))
        self.important_info['get_token'] = response['result']['token']
        logging.info(msg='get_token: ' + str(self.important_info['get_token']))

    def sso_token(self):
        """
        获取保险大厅登陆的token
        :return:
        """
        url = URL.YG_RS_URL + "/api/sso/getTtl"
        data = {
            "token": self.important_info['get_token']
        }
        # headers = {"token": self.important_info['token']}
        response = self.request_util.request(url, API.GET, API.JSON, param=data)
        logging.info(msg='response: ' + str(response))
        self.important_info['ttl'] = response['result']['ttl']
        logging.info(msg='ttl: ' + str(self.important_info['ttl']))


    def get_ttl(self):
        """
        获取保险大厅登陆的token
        :return:
        """
        login_info = {
            "username": "15726621255",
            "password": "bGl1eWlxaWFvMDcyNw==",
            "customer_code": "554443"
        }
        self.baoxian_project_login(login_info)
        self.choose_company(login_info)
        self.get_token()
        self.sso_token()
        return self.important_info['ttl']

if __name__ == '__main__':
    login_info = {"username": "13572882864", "password": "123456by", "customer_code": "503004"}
    r = logginApi()
    r.baoxian_project_login(login_info)
    r.choose_company(login_info)
    r.get_token()
    r.sso_token()
    # r.baoxian_regist()
